package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Business;
import com.example.entity.Information;
import com.example.entity.KHMS;
import com.example.mapper.BusinessMapper;
import com.example.entity.KHMS;
import com.example.service.IBusinessService;

import com.example.util.Code;
import com.example.util.QueryBean;
import com.example.util.Result;
import com.example.util.page;
import com.example.util.page4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2023-11-03
 */
@CrossOrigin
@RestController
@RequestMapping("/s/business")
public class BusinessController {
    @Autowired
    private IBusinessService businessService;
    @Autowired
    BusinessMapper businessMapper;
    @Autowired
    private OccController controller;

    //分页查询公司即对应信息
    @RequestMapping("/findpage")
    public Result findByPara(@RequestBody page Business){
        Page<Map> byParams = businessService.findByParams(Business);
        if (byParams != null) {
            return new Result(Code.GET_OK, byParams, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    @RequestMapping("/findBusinessByName")
    public Result findBusinessByName(String name){
        Business bbn = businessService.findBusinessByName(name);
        if(bbn!=null){
            return new Result(Code.GET_ERR,"fail");
        }else {
            return new Result(Code.GET_OK,bbn,"success");
        }
    }
//    TTT模糊查询
   @PostMapping("/findByBName")
    public Result findByBName(@RequestBody Business business){
       List<Business> byBName = businessService.findByBName(business);
       if(byBName!=null){
           return new Result(Code.GET_OK,byBName,"查询成功");
       }else {
           return new Result(Code.GET_ERR,"查询失败");
       }
   }




    //查询营业执照
    @RequestMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        Business byId = businessService.findByid(id);
        if (byId!=null){
            return new Result(Code.GET_OK,byId, "查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    //商户分页
    @RequestMapping("/findpages")
    public Result findpage(@RequestBody page4 Business){
        Page<Map> byParams = businessService.findpage(Business);
        if (byParams != null) {
            return new Result(Code.GET_OK, byParams, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }
    @GetMapping("/{zid}")
    public Result a(@PathVariable Integer zid){
        int i = businessMapper.addZid(zid);
        return new Result(Code.GET_OK,"成功");
    }
    @PostMapping("/add")
    public Result add(@RequestBody KHMS khms){
        int add = businessMapper.add(khms);
        return new Result(Code.GET_OK,"成功");
    }
    @PostMapping("/addzidcard/{zid}")
    public Result addIdzCard(@RequestParam MultipartFile file, @PathVariable Integer zid) throws IOException {
        Result result = controller.uploadImg(file);
        Business business = new Business();
        business.setFront((String) result.getData());
        business.setZid(zid);
        int i = businessMapper.update(business);
        return new Result(Code.GET_OK,i,"成功");
    }
    @PostMapping("/addzidcard1/{zid}")
    public Result addIdzCard1(@RequestParam MultipartFile file, @PathVariable Integer zid) throws IOException {
        Result result = controller.uploadImg(file);
        Business business = new Business();
        business.setDoor((String) result.getData());
        business.setZid(zid);
//        int update = businessMapper.update(business);
        int i = businessMapper.update1(business);
        return new Result(Code.GET_OK,"成功");
    }


//    @PostMapping("/add")
//    public Result add(@RequestBody KHMS khms){
//        int add = businessService.add(khms);
//        return new Result(Code.SAVE_OK,khms.getId(),"添加成功");
//    }
}
